<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="form"
      :rules="rules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <img src="@/assets/dikedelogo.png" alt="" />
      </div>

      <el-form-item prop="loginName">
        <span class="svg-container el-icon-mobile" />
        <el-input v-model="form.loginName" placeholder="请输入账号" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <i class="el-icon-lock"></i>
        </span>
        <el-input
          class="p-center"
          ref="pwdinfo"
          v-model="form.password"
          :type="inputType"
          placeholder="请输入密码"
        />
        <!-- <span class="svg-container">
          <span :icon-class="inputType==='password'?'eye':'eye-open' "></span>
        </span> -->
        <span class="el-icon-view"></span>
      </el-form-item>
      <el-form-item class="number-bottom" prop="code">
        <div>
          <span class="svg-container el-icon-user-solid" />
          <el-input
            class="p-bottom"
            v-model="form.code"
            placeholder="请输入验证码"
          />
        </div>
        <div class="p-number" >
          <img :src="src" alt="">
        </div>
      </el-form-item>
      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click="onsubmit"
        >登录</el-button
      >
      <div />
    </el-form>
  </div>
</template>

<script>
import { loginCodeApi } from '@/api/user'
// import axios from 'axios'
export default {
  name: "Login",
  data() {
    return {
      src:'',
      form: {
        loginName: "admin",
        password: "admin",
        clientToken: "",
        loginType: 0,
      },
      inputType: "password",
      loading: false,
      // 定义规则
      // 校验三步骤 1、绑定model和rules 动态绑定
      // 2、prop指定校验的字段
      // 3、v-model绑定
      rules: {
        // 必填
        loginName: [
          { required: true, message: "请填写账号", trigger: "blur" },
          {
            pattern: /^[a-zA-Z0-9_-]{4,16}$/,
            message: "账号格式不正确",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请填写密码", trigger: "blur" },
          { min: 5, max: 16, message: "密码长度在6-16位之间", trigger: "blur" },
        ],
        code: [
          { required: true, message: "请填写验证码", trigger: "blur"},
        ],
      },
    };
  },
  created() {
    this.loginCode ()
  },
  methods: {
    // handleLogin() {},
    // changeType() {
    //   this.inputType === "password"
    //     ? (this.inputType = "text")
    //     : (this.inputType = "password");
    // //   this.$nextTick(() => {
    // //     this.$refs.pwdinfo.focus();
    // //   });
    // },
    async loginCode () {
      try {
        const random = Math.random().toString()
        const res = await loginCodeApi(random)
        console.log(res);
        const blob = new Blob([res], { type: 'image/jpeg' })
        this.src = window.URL.createObjectURL(blob)
        // 登录需要的验证码
        this.form.clientToken = random
      } catch (err) {
        console.log(err)
      }
    },
   async onsubmit() {
    console.log(this.form);
      try {
        // this.loading = true;
        await this.$refs.loginForm.validate();
        console.log('可以调用后端接口了')
        // 触发actions
       await this.$store.dispatch("user/loginAction", this.form);
        // // 用await 保证拿到token之后变成false
        this.loading = false;
        // // 登陆成功跳转
        this.$router.push("/run");
      } catch (e) {
        console.log(e);
      }
    },
   
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
// $light_gray: #fff;
$cursor: #fff;

// @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
//   .login-container .el-input input {
//     // color: $cursor;
//   }
// }

/* reset element-ui css */
body {
  margin: 0 !important;
}
.login-container {
  height: 851px;
  padding: 0;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      // -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      //   color: $light_gray;
      height: 47px;
      //   caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.7);
    border-radius: 5px;
    color: #454545;
  }
  .el-form-item_error {
    color: #fff;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  position: relative;
  min-height: 100%;
  width: 100%;
  // ~ 模块化解析
  background-image: url("~@/assets/dikedeimg.png");
  //   背景图片大小
  background-size: cover;
  background-position: center;
  overflow: hidden;

  .el-input {
    input {
      color: #283443;
    }
  }
  //   边框
  .el-form-item.is-required {
    border: 1px solid #e2e2e2;
  }
  .login-form {
    position: relative;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    height: 312px;
    width: 488px;
    // max-width: 100%;
    padding: 76px 35px 0;
    // margin: 0 auto;
    // overflow: hidden;
    background-color: #fff;
    border-radius: 10px;
    .p-center {
      width: 82%;
    }
    .number-bottom {
      position: relative;
      .p-bottom {
        width: 85%;
      }
      .p-number {
        position: absolute;
        width: 130px;
        height: 47px;
        top: 0;
        right: 0;
        img{
          width: 100%;
          height: 100%;
        }
      }
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    // padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    // width: 30px;
    display: inline-block;
  }

  .title-container {
    position: absolute;
    top: -10%;
    left: 40%;
    img {
      width: 96px;
      height: 96px;
    }
    .el-form-item {
      .el-form-item_content {
        .svg-container {
          height: 16px;
          width: 16px;
        }
      }
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .el-button--primary {
    height: 52px;
    background: linear-gradient(262deg, #2e50e1, #6878f0);
  }
}
</style>
